/**
 * 运行时服务选择器
 * 根据环境自动选择合适的服务实现
 */

// 检查是否在 Electron 环境中
function isElectronEnvironment(): boolean {
  return typeof window !== 'undefined' && 
    typeof (window as any).electronAPI !== 'undefined'
}

// 动态导入服务
export async function getServices() {
  if (isElectronEnvironment()) {
    // Electron 环境：使用 Electron API 适配器
    const [
      { historyParser },
      { fileOperations },
      { recoveryEngine }
    ] = await Promise.all([
      import('./electron-history-parser'),
      import('./electron-file-operations'),
      import('./electron-recovery-engine')
    ])
    
    return {
      historyParser,
      fileOperations,
      recoveryEngine
    }
  } else {
    // 非 Electron 环境：使用原始实现
    const [
      { historyParser },
      { fileOperations },
      { recoveryEngine }
    ] = await Promise.all([
      import('./history-parser'),
      import('./file-operations'),
      import('./recovery-engine')
    ])
    
    return {
      historyParser,
      fileOperations,
      recoveryEngine
    }
  }
}

// 同步版本（用于已知环境）
export function getServicesSync() {
  if (isElectronEnvironment()) {
    const { historyParser } = require('./electron-history-parser')
    const { fileOperations } = require('./electron-file-operations')
    const { recoveryEngine } = require('./electron-recovery-engine')
    
    return {
      historyParser,
      fileOperations,
      recoveryEngine
    }
  } else {
    const { historyParser } = require('./history-parser')
    const { fileOperations } = require('./file-operations')
    const { recoveryEngine } = require('./recovery-engine')
    
    return {
      historyParser,
      fileOperations,
      recoveryEngine
    }
  }
}